//
//  LCInvitePosterViewController.m
//  lechuang
//
//  Created by Mr_hi on 2024/8/28.
//  Copyright © 2024 Mooyi. All rights reserved.
//

#import "LCInvitePosterViewController.h"
#import "LCInvitePosterCollectionViewCell.h"
#import "TZImageManager.h"
#import "LCInviteShareView.h"

@interface LCInvitePosterViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic,strong) NSArray *imgArr;

@property (nonatomic,assign) NSInteger totalItems;

@property (nonatomic,assign) NSUInteger currentpage;

@end

@implementation LCInvitePosterViewController {
    float _oldPoint;
    NSInteger _dragDirection;
}

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

- (void)configUI {
    if (self.collectionView.contentOffset.x == 0) {
        NSInteger targeIndex = 0;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:targeIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        _oldPoint = self.collectionView.contentOffset.x;
        self.collectionView.userInteractionEnabled = YES;
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 50, 0, 50);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 21;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat cellW = ScreenW - 100;
    CGFloat cellH = ceil(cellW * 533/300.0);
    return CGSizeMake(cellW, cellH);
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCInvitePosterCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCInvitePosterCollectionViewCell" forIndexPath:indexPath];
    cell.bgView.image = [UIImage imageNamed:kFORMAT(@"lc_invite_poster_bg%ld",indexPath.item)];
    return cell;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.collectionView.userInteractionEnabled = NO;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _oldPoint = scrollView.contentOffset.x;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scrollViewDidEndScrollingAnimation:self.collectionView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    self.collectionView.userInteractionEnabled = YES;
}

//手离开屏幕的时候
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    float currentPoint = scrollView.contentOffset.x;
    float moveWidth = currentPoint-_oldPoint;
    if (moveWidth>0) {
        _dragDirection = 1;
    }else if (moveWidth<0){
        _dragDirection = -1;
    }else{
        _dragDirection = 0;
    }
}

- (void)scrollViewWillBeginDecelerating: (UIScrollView *)scrollView {
    NSInteger currentIndex = (_oldPoint + ((ScreenW - 100) + 21) * 0.5) / (21 + (ScreenW - 100));
    
    CGFloat targetIndex = currentIndex + _dragDirection;
    if (targetIndex < 0) {
        targetIndex = 0;
    } else if (targetIndex > 3){
        targetIndex = 3;
    }
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}



-(NSInteger)currentIndex {
    if(self.collectionView.frame.size.width == 0 || self.collectionView.frame.size.height == 0) {
        return 0;
    }
    NSInteger index = 0;
    index = (self.collectionView.contentOffset.x + ((ScreenW - 100) + 21) * 0.5) / ((ScreenW - 100) + 21);
    if (index < 0) {
        index = 0;
    }
    return MAX(0,index);
}

- (IBAction)saveImgBtnAction:(UIButton *)sender {
    LCInvitePosterCollectionViewCell * cell = (LCInvitePosterCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:[self currentIndex] inSection:0]];
    UIImage * img = [UIImage imageCaptureWithView:cell];
    [[TZImageManager manager] savePhotoWithImage:img completion:^(PHAsset *asset, NSError *error) {
        if (!error && asset) {
            [self showSuccessText:@"图片已保存"];
        }
    }];
}

- (IBAction)inviteBtnAction:(UIButton *)sender {
    
    LCInvitePosterCollectionViewCell * cell = (LCInvitePosterCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:[self currentIndex] inSection:0]];
    UIImage * img = [UIImage imageCaptureWithView:cell];
    
    LCInviteShareView * view = [[LCInviteShareView alloc] initForNibWithShareImg:img];
    view.parentVC = self;
    [view show];
}

@end
